package ru.inse.realty.web.page;

import java.io.PrintWriter;
import java.util.List;

import org.springframework.context.MessageSource;

import ru.inse.realty.core.dao.RealtyQueryParam;
import ru.inse.realty.core.domain.RealtyObject;
import ru.inse.realty.core.domain.RealtyObjectType;
import ru.inse.realty.core.domain.RealtyRubric;
import ru.inse.realty.core.service.IRealtyItemService;
import ru.inse.realty.core.service.IRealtyObjectService;
import ru.inse.realty.core.service.filter.IRealtyFilter;
import ru.inse.realty.web.module.RealtyModule;
import ru.xeden.web.html.RenderContext;
import ru.xeden.web.html.component.UserComponent;
import ru.xeden.web.html.component.lib.AbstractComponent;


public class ContentComponent
    extends UserComponent
{
    public static final String ID = "content";
    private final RealtyModule module;
    private Object[] DUMMY = new Object[0];

    public ContentComponent(RealtyModule module)
    {
        this.module = module;
        addChild(new DateValue());
    }

    private class DateValue
        extends AbstractComponent
    {

        @Override
        public void toMarkup(RenderContext ctx, PrintWriter writer)
        {
            RealtyQueryParam param = new RealtyQueryParam();
            param.copyFrom(ctx.getRequest().getParameterMap());

            MessageSource messageSource = module.getMessageSource();
            IRealtyObjectService realtyObjectService = module.getRealtyObjectService();
            IRealtyItemService realtyItemService = module.getRealtyItemService();
            
            if (realtyObjectService != null)
            {
                
                for (RealtyObjectType type: realtyObjectService.getObjectTypes())
                {
                    writer.append("<table class='table table-bordered table-striped'><thead>");
                    writer.append("<tr><td style='width: 400px;'>");
                    writer.append(messageSource.getMessage(type.getLabelId(), DUMMY, ctx.getLocale()));
                    writer.append("</td>");
                    
                    RealtyRubric[] rubricList =  type.getRubrics();
                    for (RealtyRubric rubric : rubricList)
                    {
                        writer.append("<td class='rubric'>");
                        writer.append(messageSource.getMessage(rubric.getLabelId(), DUMMY, ctx.getLocale()));
                        writer.append("</td>");
                    }
                    writer.append("</tr></thead><tbody>");
                    for (RealtyObject object : realtyObjectService.getObjects(type))
                    {
                        writer.append("<tr class='objectNameRow'><td class='objectName'>");
                        writer.append("<a class='count' href='");
//                        writer.append(RealtyUrlParamBuilder.buildUrl(city.getId(), null, type, object, request))
//                        .append("?")
//                        .append(urlParamBuilder.getPage());
                        writer.append("'>");
                        writer.append(messageSource.getMessage(object.getLabelId(), DUMMY, ctx.getLocale()));
                        writer.append("</a></td>");
                        for (RealtyRubric rubric : rubricList)
                        {
                            writer.append("<td class='count'>");
                            Long count = realtyItemService.count(param, rubric, object);
                            if (count == null || count.intValue() == 0)
                            {
                                writer.append("-");
                            }
                            else
                            {
                                writer.append("<a href='");
                                writer.append(RealtyUrlParamBuilder.buildUrl(param, rubric, type, object))
                                .append("?");
//                                .append(urlParamBuilder.getPage());
                                writer.append("'>");
                                writer.append(String.valueOf(count));
                                writer.append("</a>");
                            }
                            writer.append("</td>");
                        }
                        
                        
                        List<IRealtyFilter> filterList = realtyObjectService.getFilters(object);
                        for (IRealtyFilter filter : filterList)
                        {
                            writer.append("<tr class='filterRow'><td class='filter'>");
                            writer.append("<a href='");
                            writer.append(RealtyUrlParamBuilder.buildUrl(param, null, type, object));
                            writer.append("?");
                            writer.append(param.getPage().toString());
                            writer.append("&");
//                            writer.append(RealtyUrlParamBuilder.buildSearch(filter.getRealtyQueryParam()));
                            writer.append("'>");
                            writer.append(messageSource.getMessage(filter.getLabelId(), DUMMY, filter.getLabelId(), ctx.getLocale()));
                            writer.append("</a></td>");
                            for (RealtyRubric rubric : rubricList)
                            {
                                writer.append("<td class='count'>");
                                Long count = realtyItemService.count(filter.makeParam(param), rubric, object);
                                if (count == null || count.intValue() == 0)
                                {
                                    writer.append("-");
                                }
                                else
                                {
                                    writer.append("<a href='");
                                    writer.append(RealtyUrlParamBuilder.buildUrl(param, rubric, type, object))
                                    .append("?")
//                                    .append(urlParamBuilder.getPage())
                                    .append("&");
//                                    .append(RealtyUrlParamBuilder.buildSearch(filter.getRealtyQueryParam()));
                                    writer.append("'>");
                                    writer.append(String.valueOf(count));
                                    writer.append("</a>");
                                }
                            writer.append("</td>");
                                
                            }
                        writer.append("</td></tr>");
                        }
                        writer.append("</td></tr>");
                    }
                    writer.append("</tbody></table>");

                }
                writer.append("</table>");
            }
            else
            {
                writer.append(String.valueOf("realtyObjectService is null"));
            }
        }
    }
}
